package ks.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.List;

import ks.dao.KidDAO;
import ks.dao.TeacherDAO;
import ks.po.Kid;
import ks.dao.TeachingDAO;
import ks.po.Teacher;
import ks.po.Teaching;

//@WebServlet(name = "TeachingServlet")
@WebServlet("/TeachingServlet")
public class TeachingServlet extends HttpServlet
{
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String mode = request.getParameter("mode");
        if("search".equals(mode))
        {
            searchKid(request, response);
        }
        else if("saveCaseHistory".equals(mode))
        {
            saveCaseHistory(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String mode = request.getParameter("mode");
        if ("showCaseHistory".equals(mode))//浏览病历
        {
            showCaseHistory(request, response);
        }
        else if ("addCaseHistory".equals(mode))
        {
            addCaseHistory(request, response);
        }
    }

    private void searchKid(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
    {
        String kidName = request.getParameter("kidName");
        String ownerName = request.getParameter("ownerName");
        try
        {
//            List<Kid> kids = new KidDAO().search(kidName, "");
            List<Kid> kids = new KidDAO().search(kidName, ownerName);
            if(0==kids.size())
            {
                request.setAttribute("msg", "没有查到幼儿信息");
                request.getRequestDispatcher("/teachingSearch.jsp").forward(request, response);
            }
            else
            {
                request.setAttribute("kids", kids);
                request.getRequestDispatcher("/teachingSearchResult.jsp").forward(request, response);
            }
        }
        catch (Exception e)
        {
            request.setAttribute("msg", e.getMessage());
            request.getRequestDispatcher("/teachingSearch.jsp").forward(request, response);
        }
    }

    private void showCaseHistory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        try
        {
            TeachingDAO teachingDAO = new TeachingDAO();
            List<Teaching> teachings = teachingDAO.getTeachingsByKidId(Integer.parseInt(request.getParameter("kidId")));
            request.setAttribute("teachings", teachings);
            if (0 == teachings.size())
            {
                request.setAttribute("msg", "没有找到历史病历");
            }
            request.getRequestDispatcher("/showCaseHistory.jsp").forward(request, response);
        }
        catch (Exception e)
        {
            request.setAttribute("msg", e.getMessage());
            request.getRequestDispatcher("/teachingSearch.jsp").forward(request, response);
        }
    }

    private void addCaseHistory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        try
        {
            TeacherDAO teacherDAO = new TeacherDAO();
            List<Teacher> teachers = teacherDAO.getAll();
            request.setAttribute("teachers", teachers);
            request.getRequestDispatcher("/addCaseHistory.jsp").forward(request, response);
        }
        catch (Exception e)
        {
            request.setAttribute("msg", e.getMessage());
            request.getRequestDispatcher("/teachingSearch.jsp").forward(request, response);
        }
    }

    private void saveCaseHistory(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        try
        {
            Teaching teaching = new Teaching();
            teaching.setKidId(Integer.parseInt(request.getParameter("pid")));
            teaching.setTeacherId(Integer.parseInt(request.getParameter("vid")));
            //缺少teachDate,原因是在teachingDAO.save()中用MySQL的CURDATE()函数获取当前日期
            teaching.setEffect(request.getParameter("effect"));
            teaching.setTreatment(request.getParameter("treatment"));
            TeachingDAO teachingDAO = new TeachingDAO();
            teachingDAO.save(teaching);
            String kidName = request.getParameter("kidName");
            request.setAttribute("msg", "幼儿"+ kidName +"的病历添加成功");
//            response.sendRedirect("CustomerServlet?mode=saveTeaching&cid="+request.getParameter("cid"));
            request.getRequestDispatcher("/teachingSearch.jsp").forward(request, response);
        }
        catch (Exception e)
        {
            request.setAttribute("msg", e.getMessage());
            request.getRequestDispatcher("/teachingSearch.jsp").forward(request, response);
        }
    }
}
